using System.Globalization;
using Microsoft.Extensions.Configuration;
using Wechat.Service.Extractor;
using Wechat.Service.Filter;
using Wechat.Settings;

namespace Wechat.Service.Convert;

public class ConvertService : IConvertService
{
    private readonly IExtractorService _extractorService;
    private readonly IConfiguration _configuration;
    private readonly IConfigurationSection _wechatFolderSection;
    private readonly IFilterService _filterService;

    public ConvertService(
        IExtractorService extractorService,
        IConfiguration configuration,
        IFilterService filterService
    )
    {
        _extractorService = extractorService;
        _configuration = configuration;
        _wechatFolderSection =
            _configuration.GetSection(AppSettings.WechatFolder)
            ?? throw new Exception($"{AppSettings.WechatFolder}在appsettings.json没有发现");
        _filterService = filterService;
    }

    public async void ConvertToOneText(List<string> paths, string name)
    {
        // Console.WriteLine(name);
        // Console.WriteLine(paths.FirstOrDefault());
        // Console.WriteLine(Path.GetDirectoryName(paths.FirstOrDefault()));
        // Console.WriteLine(Path.Combine(Path.GetDirectoryName(paths.FirstOrDefault()) ?? "", name));
        // Console.WriteLine(_configuration.GetSection());
        // System.Console.WriteLine(_wechatFolderSection.GetSection(AppSettings.TxtFolder).Value);
        var txtFolder =
            _wechatFolderSection.GetSection(AppSettings.TxtFolder).Value
            ?? throw new Exception($"{AppSettings.TxtFolder}在appsettings.json没有发现");

        if (!Directory.Exists(txtFolder))
            Directory.CreateDirectory(txtFolder);

        // System.Console.WriteLine(txtFolder);
        // System.Console.WriteLine(Path.Combine(txtFolder,name,".txt"));
        var txtPath = Path.Combine(txtFolder, name + ".txt");
        using StreamWriter writer = new StreamWriter(txtPath);
        // System.Console.WriteLine(paths.Count);
        // System.Console.WriteLine(name);
        var i = 1;
        var idxStart = 0;
        var idxEnd = 0;
        foreach (var path in paths)
        {
            // System.Console.WriteLine(Path.GetFileNameWithoutExtension(path).Substring(16));
            // var title = Path.GetFileNameWithoutExtension(path).Substring(16);
            // System.Console.WriteLine(path);
            var content = await _extractorService.ExtractContentFromHtmlByPathAsync(path);
            if (content.Contains("-----start-----"))
            {
                System.Console.WriteLine($"start = {idxStart++}");
            }
            else
            {
                System.Console.WriteLine($"no_start->{path}");
            }
            if (content.Contains("-----end-----"))
            {
                System.Console.WriteLine($"end = {idxEnd++}");
            }
            else
            {
                System.Console.WriteLine($"no_end->{path}");
            }

            var realContent =
                content
                    .Split("-----start-----")
                    .LastOrDefault()
                    ?.Split("-----end-----")
                    .FirstOrDefault() ?? throw new Exception("无法获取中间(start-end)的内容");

            var formatContent = _filterService.FormatContent(realContent);
            // System.Console.WriteLine(content);
            // Console.WriteLine(Path.GetFileNameWithoutExtension(path));
            // Console.WriteLine(Path.GetFileNameWithoutExtension(path).Substring(0,15));
            // System.Console.WriteLine(DateTime.ParseExact(Path.GetFileNameWithoutExtension(path).Substring(0,15),"yyyyMMdd_HHmmss",CultureInfo.InstalledUICulture));

            // var publishTime = DateTime.ParseExact(Path.GetFileNameWithoutExtension(path).Substring(0, 15), "yyyyMMdd_HHmmss", CultureInfo.InstalledUICulture).ToString("yyyy年MM月dd日_HH点mm分ss秒");

            // System.Console.WriteLine(publishTime);
            // System.Console.WriteLine(title);
            // System.Console.WriteLine(publishTime);
            // System.Console.WriteLine($"{i}\n");
            // 使用StreamWriter打开文件
            // 逐行写入内容
            writer.WriteLine($"第{i++}章\n");
            // writer.WriteLine($"{publishTime} {title}\n");
            // writer.WriteLine($"{publishTime}\n");
            writer.WriteLine(formatContent + "\n");
        }
    }

    public void ConvertToOneTextWithFilter(List<string> paths, string name)
    {
        throw new NotImplementedException();
    }

    public void ConvertToText(string path)
    {
        throw new NotImplementedException();
    }

    public void ConvertToOnePDF(List<string> paths, string name)
    {
        var txtFolder =
            _wechatFolderSection.GetSection(AppSettings.TxtFolder).Value
            ?? throw new Exception($"{AppSettings.TxtFolder}在appsettings.json没有发现");

        if (!Directory.Exists(txtFolder))
            Directory.CreateDirectory(txtFolder);

        foreach (var path in paths)
        {
            System.Console.WriteLine(path);
        }
    }
}
